//
//  SqQueue.cpp
//  DataStructure
//
//  Created by 舒磊明 on 2020/6/26.
//  Copyright © 2020 Shuleiming. All rights reserved.
//

#include "SqQueue.hpp"

// 初始化
void InitQueue(SqQueue &Q)
{
    Q.front = 0;
    Q.rare = 0;
}

// 判断队空
bool QueueEmpty(SqQueue Q)
{
    return Q.front == Q.rare;
}

// 判断队满
bool QueueFull(SqQueue Q)
{
    return (Q.rare + 1) % MAX_SIZE == Q.front;
}

// 入队
bool EnQueue(SqQueue &Q, int x)
{
    if (QueueFull(Q)) {
        return false;
    }
    Q.data[Q.rare] = x;
    Q.rare = (Q.rare + 1) % MAX_SIZE;
    return true;
}

// 出队
bool DeQueue(SqQueue &Q, int &x)
{
    if (QueueEmpty(Q)) {
        return false;
    }
    x = Q.data[Q.front];
    Q.front = (Q.front + 1) % MAX_SIZE;
    return true;
}

// 获取队列元素的个数
int QueueElem(SqQueue Q)
{
    return (Q.rare - Q.front + MAX_SIZE) % MAX_SIZE;
}
